package com.leopardcat.firewall;

import java.lang.reflect.Method;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.BroadcastReceiver;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.RemoteException;
import android.telephony.SmsMessage;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

import com.android.internal.telephony.ITelephony;

public class CallFireWallActivity extends Activity {
	// block number
	private final static String BLOCKED_NUMBER = "10000";
	
	private IncomingCallReceiver mIncomingCallReceiver;
	private ITelephony			 mITelephony;
	private AudioManager		 mAudioManager;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mIncomingCallReceiver = new IncomingCallReceiver();
        
        IntentFilter mIntentFilter = new IntentFilter();
        
        mIntentFilter.addAction("android.intent.action.PHONE_STATE");
        mIntentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
        
        registerReceiver(mIncomingCallReceiver, mIntentFilter);
        mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
        
        //
        TelephonyManager mTelephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
        try{
        	Method getITelephonyMethod = TelephonyManager.class.getDeclaredMethod("getITelephony", (Class[]) null);
        	getITelephonyMethod.setAccessible( true );
        	mITelephony = (ITelephony) getITelephonyMethod.invoke(mTelephonyManager, (Object[]) null);
        	
        } catch ( Exception e) {
        	e.printStackTrace();
        }
        
    }
    
    /** incoming call receiver */
    private class IncomingCallReceiver extends BroadcastReceiver {
    	/** */
    	@Override
    	public void onReceive(Context context, Intent intent) {
    		String action = intent.getAction();
    		Log.d("leopard", "Action:" + action);
    		
    		if( action.equals("android.intent.action.PHONE_STATE") ) {
    			Log.d("leopard", "incoming call...");
    			String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
    			
    			String number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
    			
    			if( state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING) ) {
    				if( number.equals(BLOCKED_NUMBER) ) {
    					// mute sound
    					mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
    					
    					// hangout incoming
    					try {
							mITelephony.endCall();
						} catch (RemoteException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
    					
    					mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
    				}
    			}
    			
    		} else if ( action.equals("android.provider.Telephony.SMS_RECEIVED")) {
				SmsMessage sms = getMessagesFromIntent(intent)[0];
				String number = sms.getOriginatingAddress();
				String content = sms.getDisplayMessageBody();
				Log.d("leopard", "incoming number: " + number);
				if( number.equals("10000") ) {
					Toast.makeText(context, number+content, Toast.LENGTH_LONG).show();
					abortBroadcast();
				}
			}
			
    	}
    }
    
    public final static SmsMessage[] getMessagesFromIntent(Intent intent) {
    	Object[] messages = (Object[]) intent.getSerializableExtra("pdus");
    	byte [][] pduObjs = new byte [messages.length][];
    	for( int i = 0; i < messages.length; i++ ) {
    		pduObjs[i] = (byte[]) messages[i];
    	}
    	
    	byte [][] pdus = new byte[pduObjs.length][];
    	int pduCount = pdus.length;
    	SmsMessage[] msgs = new SmsMessage[pduCount];
    	
    	for( int i = 0; i < pduCount; i++ ) {
    		pdus[i] = pduObjs[i];
    		msgs[i] = SmsMessage.createFromPdu(pdus[i]);
    	}
    	
    	return msgs;
    }
   
    
}

